home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / src-tk / shapes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  12.1 KB  |  456 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "tk.h"
  6. #include "private.h"
  7.  
  8. /******************************************************************************/
  9.  
  10. #define PI 3.14159265358979323846
  11.  
  12. /******************************************************************************/
  13.  
  14. void tkWireSphere(GLuint base, float radius)
  15. {
  16.     GLUquadricObj *quadObj;
  17.  
  18.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  19.     quadObj = gluNewQuadric();
  20.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  21.     gluSphere(quadObj, radius, 16, 16);
  22.     glEndList();
  23. }
  24.  
  25. /******************************************************************************/
  26.  
  27. void tkSolidSphere(GLuint base, float radius)
  28. {
  29.     GLUquadricObj *quadObj;
  30.  
  31.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  32.     quadObj = gluNewQuadric();
  33.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  34.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  35.     gluSphere(quadObj, radius, 16, 16);
  36.     glEndList();
  37. }
  38.  
  39. /******************************************************************************/
  40.  
  41. void tkWireCube(GLuint base, float size)
  42. {
  43.     static float n[6][3] = {
  44.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  45.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  46.     };
  47.     static GLint faces[6][4] = {
  48.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  49.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  50.     };
  51.     float x0, x1, y0, y1, z0, z1, tmp;
  52.     float v[8][3];
  53.     int i;
  54.  
  55.     x0 = -size / 2.0;
  56.     x1 = size / 2.0;
  57.     y0 = -size / 2.0;
  58.     y1 = size / 2.0;
  59.     z0 = -size / 2.0;
  60.     z1 = size / 2.0;
  61.  
  62.     if (x0 > x1) {
  63.     tmp = x0; x0 = x1; x1 = tmp;
  64.     }
  65.     if (y0 > y1) {
  66.     tmp = y0; y0 = y1; y1 = tmp; 
  67.     }
  68.     if (z0 > z1) {
  69.     tmp = z0; z0 = z1; z1 = tmp; 
  70.     }
  71.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  72.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  73.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  74.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  75.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  76.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  77.  
  78.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  79.     for (i = 0; i < 6; i++) {
  80.         glBegin(GL_LINE_LOOP);
  81.         glNormal3fv(&n[i][0]);
  82.         glVertex3fv(&v[faces[i][0]][0]);
  83.         glNormal3fv(&n[i][0]);
  84.         glVertex3fv(&v[faces[i][1]][0]);
  85.         glNormal3fv(&n[i][0]);
  86.         glVertex3fv(&v[faces[i][2]][0]);
  87.         glNormal3fv(&n[i][0]);
  88.         glVertex3fv(&v[faces[i][3]][0]);
  89.         glEnd();
  90.     }
  91.     glEndList();
  92. }
  93.  
  94. /******************************************************************************/
  95.  
  96. void tkSolidCube(GLuint base, float size)
  97. {
  98.     static float n[6][3] = {
  99.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  100.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  101.     };
  102.     static GLint faces[6][4] = {
  103.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  104.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  105.     };
  106.     float x0, x1, y0, y1, z0, z1, tmp;
  107.     float v[8][3];
  108.     int i;
  109.  
  110.     x0 = -size / 2.0;
  111.     x1 = size / 2.0;
  112.     y0 = -size / 2.0;
  113.     y1 = size / 2.0;
  114.     z0 = -size / 2.0;
  115.     z1 = size / 2.0;
  116.  
  117.     if (x0 > x1) {
  118.     tmp = x0; x0 = x1; x1 = tmp;
  119.     }
  120.     if (y0 > y1) {
  121.     tmp = y0; y0 = y1; y1 = tmp; 
  122.     }
  123.     if (z0 > z1) {
  124.     tmp = z0; z0 = z1; z1 = tmp; 
  125.     }
  126.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  127.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  128.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  129.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  130.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  131.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  132.  
  133.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  134.     for (i = 0; i < 6; i++) {
  135.         glBegin(GL_POLYGON);
  136.         glNormal3fv(&n[i][0]);
  137.         glVertex3fv(&v[faces[i][0]][0]);
  138.         glNormal3fv(&n[i][0]);
  139.         glVertex3fv(&v[faces[i][1]][0]);
  140.         glNormal3fv(&n[i][0]);
  141.         glVertex3fv(&v[faces[i][2]][0]);
  142.         glNormal3fv(&n[i][0]);
  143.         glVertex3fv(&v[faces[i][3]][0]);
  144.         glEnd();
  145.     }
  146.     glEndList();
  147. }
  148.  
  149. /******************************************************************************/
  150.  
  151. void tkWireBox(GLuint base, float width, float height, float depth)
  152. {
  153.     static float n[6][3] = {
  154.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  155.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  156.     };
  157.     static GLint faces[6][4] = {
  158.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  159.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  160.     };
  161.     float x0, x1, y0, y1, z0, z1, tmp;
  162.     float v[8][3];
  163.     int i;
  164.  
  165.     x0 = -width / 2.0;
  166.     x1 = width / 2.0;
  167.     y0 = -height / 2.0;
  168.     y1 = height / 2.0;
  169.     z0 = -depth / 2.0;
  170.     z1 = depth / 2.0;
  171.  
  172.     if (x0 > x1) {
  173.     tmp = x0; x0 = x1; x1 = tmp;
  174.     }
  175.     if (y0 > y1) {
  176.     tmp = y0; y0 = y1; y1 = tmp; 
  177.     }
  178.     if (z0 > z1) {
  179.     tmp = z0; z0 = z1; z1 = tmp; 
  180.     }
  181.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  182.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  183.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  184.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  185.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  186.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  187.  
  188.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  189.     for (i = 0; i < 6; i++) {
  190.         glBegin(GL_LINE_LOOP);
  191.         glNormal3fv(&n[i][0]);
  192.         glVertex3fv(&v[faces[i][0]][0]);
  193.         glNormal3fv(&n[i][0]);
  194.         glVertex3fv(&v[faces[i][1]][0]);
  195.         glNormal3fv(&n[i][0]);
  196.         glVertex3fv(&v[faces[i][2]][0]);
  197.         glNormal3fv(&n[i][0]);
  198.         glVertex3fv(&v[faces[i][3]][0]);
  199.         glEnd();
  200.     }
  201.     glEndList();
  202. }
  203.  
  204. /******************************************************************************/
  205.  
  206. void tkSolidBox(GLuint base, float width, float height, float depth)
  207. {
  208.     static float n[6][3] = {
  209.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  210.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  211.     };
  212.     static GLint faces[6][4] = {
  213.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  214.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  215.     };
  216.     float x0, x1, y0, y1, z0, z1, tmp;
  217.     float v[8][3];
  218.     int i;
  219.  
  220.     x0 = -width / 2.0;
  221.     x1 = width / 2.0;
  222.     y0 = -height / 2.0;
  223.     y1 = height / 2.0;
  224.     z0 = -depth / 2.0;
  225.     z1 = depth / 2.0;
  226.  
  227.     if (x0 > x1) {
  228.     tmp = x0; x0 = x1; x1 = tmp;
  229.     }
  230.     if (y0 > y1) {
  231.     tmp = y0; y0 = y1; y1 = tmp; 
  232.     }
  233.     if (z0 > z1) {
  234.     tmp = z0; z0 = z1; z1 = tmp; 
  235.     }
  236.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  237.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  238.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  239.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  240.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  241.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  242.  
  243.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  244.     for (i = 0; i < 6; i++) {
  245.         glBegin(GL_POLYGON);
  246.         glNormal3fv(&n[i][0]);
  247.         glVertex3fv(&v[faces[i][0]][0]);
  248.         glNormal3fv(&n[i][0]);
  249.         glVertex3fv(&v[faces[i][1]][0]);
  250.         glNormal3fv(&n[i][0]);
  251.         glVertex3fv(&v[faces[i][2]][0]);
  252.         glNormal3fv(&n[i][0]);
  253.         glVertex3fv(&v[faces[i][3]][0]);
  254.         glEnd();
  255.     }
  256.     glEndList();
  257. }
  258.  
  259. /******************************************************************************/
  260.  
  261. void tkWireTorus(GLuint base, float innerRadius, float outerRadius)
  262. {
  263.     GLint i, j;
  264.     float theta1, phi1, theta2, phi2, rings, sides;
  265.     float v0[03], v1[3], v2[3], v3[3];
  266.     float n0[3], n1[3], n2[3], n3[3];
  267.  
  268.     rings = 5;
  269.     sides = 10;
  270.  
  271.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  272.     for (i = 0; i < rings; i++) {
  273.     theta1 = (float)i * 2.0 * PI / rings;
  274.     theta2 = (float)(i + 1) * 2.0 * PI / rings;
  275.     for (j = 0; j < sides; j++) {
  276.         phi1 = (float)j * 2.0 * PI / sides;
  277.         phi2 = (float)(j + 1) * 2.0 * PI / sides;
  278.  
  279.         v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  280.         v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  281.         v0[2] = innerRadius * sin(phi1);
  282.  
  283.         v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  284.         v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  285.         v1[2] = innerRadius * sin(phi1);
  286.  
  287.         v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  288.         v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  289.         v2[2] = innerRadius * sin(phi2);
  290.  
  291.         v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  292.         v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  293.         v3[2] = innerRadius * sin(phi2);
  294.  
  295.         n0[0] = cos(theta1) * (cos(phi1));
  296.         n0[1] = -sin(theta1) * (cos(phi1));
  297.         n0[2] = sin(phi1);
  298.  
  299.         n1[0] = cos(theta2) * (cos(phi1));
  300.         n1[1] = -sin(theta2) * (cos(phi1));
  301.         n1[2] = sin(phi1);
  302.  
  303.         n2[0] = cos(theta2) * (cos(phi2));
  304.         n2[1] = -sin(theta2) * (cos(phi2));
  305.         n2[2] = sin(phi2);
  306.  
  307.         n3[0] = cos(theta1) * (cos(phi2));
  308.         n3[1] = -sin(theta1) * (cos(phi2));
  309.         n3[2] = sin(phi2);
  310.  
  311.         glBegin(GL_LINE_LOOP);
  312.         glNormal3fv(n3);
  313.         glVertex3fv(v3);
  314.         glNormal3fv(n2);
  315.         glVertex3fv(v2);
  316.         glNormal3fv(n1);
  317.         glVertex3fv(v1);
  318.         glNormal3fv(n0);
  319.         glVertex3fv(v0);
  320.         glEnd();
  321.     }
  322.     }
  323.     glEndList();
  324. }
  325.  
  326. /******************************************************************************/
  327.  
  328. void tkSolidTorus(GLuint base, float innerRadius, float outerRadius)
  329. {
  330.     GLint i, j;
  331.     float theta1, phi1, theta2, phi2, rings, sides;
  332.     float v0[03], v1[3], v2[3], v3[3];
  333.     float n0[3], n1[3], n2[3], n3[3];
  334.  
  335.     rings = 5;
  336.     sides = 10;
  337.  
  338.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  339.     for (i = 0; i < rings; i++) {
  340.     theta1 = (float)i * 2.0 * PI / rings;
  341.     theta2 = (float)(i + 1) * 2.0 * PI / rings;
  342.     for (j = 0; j < sides; j++) {
  343.         phi1 = (float)j * 2.0 * PI / sides;
  344.         phi2 = (float)(j + 1) * 2.0 * PI / sides;
  345.  
  346.         v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  347.         v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  348.         v0[2] = innerRadius * sin(phi1);
  349.  
  350.         v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  351.         v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  352.         v1[2] = innerRadius * sin(phi1);
  353.  
  354.         v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  355.         v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  356.         v2[2] = innerRadius * sin(phi2);
  357.  
  358.         v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  359.         v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  360.         v3[2] = innerRadius * sin(phi2);
  361.  
  362.         n0[0] = cos(theta1) * (cos(phi1));
  363.         n0[1] = -sin(theta1) * (cos(phi1));
  364.         n0[2] = sin(phi1);
  365.  
  366.         n1[0] = cos(theta2) * (cos(phi1));
  367.         n1[1] = -sin(theta2) * (cos(phi1));
  368.         n1[2] = sin(phi1);
  369.  
  370.         n2[0] = cos(theta2) * (cos(phi2));
  371.         n2[1] = -sin(theta2) * (cos(phi2));
  372.         n2[2] = sin(phi2);
  373.  
  374.         n3[0] = cos(theta1) * (cos(phi2));
  375.         n3[1] = -sin(theta1) * (cos(phi2));
  376.         n3[2] = sin(phi2);
  377.  
  378.         glBegin(GL_POLYGON);
  379.         glNormal3fv(n3);
  380.         glVertex3fv(v3);
  381.         glNormal3fv(n2);
  382.         glVertex3fv(v2);
  383.         glNormal3fv(n1);
  384.         glVertex3fv(v1);
  385.         glNormal3fv(n0);
  386.         glVertex3fv(v0);
  387.         glEnd();
  388.     }
  389.     }
  390.     glEndList();
  391. }
  392.  
  393. /******************************************************************************/
  394.  
  395. void tkWireCylinder(GLuint base, float radius, float height)
  396. {
  397.     GLUquadricObj *quadObj;
  398.  
  399.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  400.     glPushMatrix();
  401.     glRotatef(90.0, 1.0, 0.0, 0.0);
  402.     glTranslatef(0.0, 0.0, -1.0);
  403.     quadObj = gluNewQuadric();
  404.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  405.     gluCylinder(quadObj, radius, radius, height, 12, 2);
  406.     glPopMatrix();
  407.     glEndList();
  408. }
  409.  
  410. /******************************************************************************/
  411.  
  412. void tkSolidCylinder(GLuint base, float radius, float height)
  413. {
  414.     GLUquadricObj *quadObj;
  415.  
  416.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  417.     glPushMatrix();
  418.     glRotatef(90.0, 1.0, 0.0, 0.0);
  419.     glTranslatef(0.0, 0.0, -1.0);
  420.     quadObj = gluNewQuadric();
  421.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  422.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  423.     gluCylinder(quadObj, radius, radius, height, 12, 2);
  424.     glPopMatrix();
  425.     glEndList();
  426. }
  427.  
  428. /******************************************************************************/
  429.  
  430. void tkWireCone(GLuint base, float b, float h)
  431. {
  432.     GLUquadricObj *quadObj;
  433.  
  434.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  435.     quadObj = gluNewQuadric();
  436.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  437.     gluCylinder(quadObj, b, 0.0, h, 15, 10);
  438.     glEndList();
  439. }
  440.  
  441. /******************************************************************************/
  442.  
  443. void tkSolidCone(GLuint base, float b, float h)
  444. {
  445.     GLUquadricObj *quadObj;
  446.  
  447.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  448.     quadObj = gluNewQuadric();
  449.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  450.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  451.     gluCylinder(quadObj, b, 0.0, h, 15, 10);
  452.     glEndList();
  453. }
  454.  
  455. /******************************************************************************/
  456.